﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Flexibles.CUiFLEXAttachmentFrameWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Flexibles;

public class CUiFLEXAttachmentFrameWindow : TxWindow, IComponentConnector
{
  internal Grid createCableGrid;
  internal TextBlock labelAttachmentPart;
  internal TxObjEditBoxControl attachToEditor;
  internal Button btnOk;
  internal Button btnClose;
  private bool _contentLoaded;

  public CUiFLEXAttachmentFrameWindow(
    ICApFLEXCableAttributeViewModel attributeViewModel,
    FrameSelectionType frameSelectionType)
  {
    this.InitializeComponent();
    this.FrameType = frameSelectionType;
    this.AttributeViewModel = attributeViewModel;
    this.InitAttachmentEditor();
    this.InitAttachmentObject();
  }

  private FrameSelectionType FrameType { get; set; }

  public ICApFLEXCableAttributeViewModel AttributeViewModel { get; set; }

  private CApFLEXAttachFrameValidator FrameValidator { get; set; }

  public ITxObject AttachObjectTo { get; set; }

  private void InitAttachmentObject()
  {
    switch (this.FrameType)
    {
      case FrameSelectionType.StartFrame:
        this.AttachObjectTo = this.AttributeViewModel.StartAttachPartTo;
        break;
      case FrameSelectionType.EndFrame:
        this.AttachObjectTo = this.AttributeViewModel.EndAttachPartTo;
        break;
    }
  }

  private void InitAttachmentEditor()
  {
    this.FrameValidator = new CApFLEXAttachFrameValidator(this.AttributeViewModel.CableScopeStatus);
    this.attachToEditor.SetValidator((ITxValidator) this.FrameValidator);
    this.attachToEditor.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.AttachmentEditBoxCtrl_Picked);
    ((UIElement) this.attachToEditor).KeyUp += new KeyEventHandler(this.AttachToEditor_KeyUp);
    this.attachToEditor.Focus();
  }

  private void SaveAttachPartTo()
  {
    switch (this.FrameType)
    {
      case FrameSelectionType.StartFrame:
        this.AttributeViewModel.StartAttachPartTo = this.AttachObjectTo;
        break;
      case FrameSelectionType.EndFrame:
        this.AttributeViewModel.EndAttachPartTo = this.AttachObjectTo;
        break;
    }
  }

  private void AttachmentEditBoxCtrl_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    ITxObject itxObject = args.Object;
    if (itxObject == null)
      return;
    this.AttachObjectTo = itxObject;
  }

  private void AttachToEditor_KeyUp(object sender, KeyEventArgs e)
  {
    if (e.Key != Key.Back && e.Key != Key.Delete)
      return;
    TxObjEditBoxCtrl txObjEditBoxCtrl = sender as TxObjEditBoxCtrl;
    if (sender != this.attachToEditor)
      return;
    this.attachToEditor.Text = "";
    this.AttachObjectTo = (ITxObject) null;
  }

  private void TxWindow_Closing(object sender, CancelEventArgs e)
  {
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    switch (this.FrameType)
    {
      case FrameSelectionType.StartFrame:
        // ISSUE: explicit non-virtual call
        __nonvirtual (((Window) this).Title) = CApFLEXStringTable.TitleAttachToStartFrame;
        break;
      case FrameSelectionType.EndFrame:
        // ISSUE: explicit non-virtual call
        __nonvirtual (((Window) this).Title) = CApFLEXStringTable.TitleAttachToEndFrame;
        break;
    }
    this.attachToEditor.Object = this.AttachObjectTo;
  }

  private void btnOk_Click(object sender, RoutedEventArgs e)
  {
    this.SaveAttachPartTo();
    ((Window) this).Close();
  }

  private void btnCancel_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/windows/cuiflexattachmentframewindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closing += new CancelEventHandler(this.TxWindow_Closing);
        break;
      case 2:
        this.createCableGrid = (Grid) target;
        break;
      case 3:
        this.labelAttachmentPart = (TextBlock) target;
        break;
      case 4:
        this.attachToEditor = (TxObjEditBoxControl) target;
        break;
      case 5:
        this.btnOk = (Button) target;
        this.btnOk.Click += new RoutedEventHandler(this.btnOk_Click);
        break;
      case 6:
        this.btnClose = (Button) target;
        this.btnClose.Click += new RoutedEventHandler(this.btnCancel_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
